package com.example.viewpager_demo;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
/*
引导页只有当用户安装APP后第一次启动时出现
当用户每次启动时，判断它是否是第一次启动，如果是就开启引导页 并且  在SharedPreferences中作一个标记，如果不是直接踽主界面
 */
public class LeadingActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener
{
    TextView mTvSkip;
    ViewPager mViewPager;
    private final static int[] LEADING_IMAGE_RESOURCES = new int[]{R.drawable.b1,R.drawable.b2,R.drawable.b3};
    private ImageView mDotViews[] = null;
    private LinearLayout mDotContainer;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leading);

        //初始化控件
        initWidget();
    }

    //初始化控件
    private void initWidget()
    {
        mViewPager = (ViewPager) findViewById(R.id.welcomePager);
        //初始化
        SimplePagerAdapter adapter = new SimplePagerAdapter();
        adapter.addView(initViewList());
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(this);

        //初始化滚动的圆点
        mDotContainer = (LinearLayout) findViewById(R.id.ll_indicator);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,0,10,0);
        mDotViews = new ImageView[LEADING_IMAGE_RESOURCES.length];
        for (int i = 0; i < LEADING_IMAGE_RESOURCES.length; i++)
        {
            ImageView dotView = new ImageView(this);
            dotView.setImageResource(i==0?R.drawable.shape_point_red:R.drawable.shape_point_gray);
            dotView.setLayoutParams(params);

            //增加事件
            dotView.setTag(i);
            dotView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    int position = (int) v.getTag();
                    mViewPager.setCurrentItem(position);
                }
            });

            mDotViews[i] = dotView;
            mDotContainer.addView(dotView);
        }


    }

    //初始化ViewPager的视图
    private List<View> initViewList()
    {
        List<View> viewList = new ArrayList<View>();
        for (int i = 0; i < LEADING_IMAGE_RESOURCES.length; i++)
        {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(LEADING_IMAGE_RESOURCES[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            viewList.add(imageView);
        }
        return viewList;
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
    {

    }

    @Override
    public void onPageSelected(int position)
    {
        for (int i = 0; i < LEADING_IMAGE_RESOURCES.length; i++)
        {
            mDotViews[i].setImageResource(i==position?R.drawable.shape_point_red:R.drawable.shape_point_gray);
        }

        if(position==LEADING_IMAGE_RESOURCES.length-1)
        {
            Toast.makeText(this,"最后一页，你可以显示某个控件，让用户跳往别的界面",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state)
    {

    }
}
